package com.qiye.pointsserver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiye.pointsserver.common.PageUtil.PageParam;
import com.qiye.pointsserver.common.PageUtil.PageResult;
import com.qiye.pointsserver.mapper.UserMapper;
import com.qiye.pointsserver.model.entity.User;
import com.qiye.pointsserver.model.entity.Userinfo;
import com.qiye.pointsserver.model.vo.UserInfoVO;
import com.qiye.pointsserver.service.EmailService;
import com.qiye.pointsserver.service.UserInfoRepository;
import com.qiye.pointsserver.service.UserinfoService;
import com.qiye.pointsserver.mapper.UserinfoMapper;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【sys_userinfo(用户信息表)】的数据库操作Service实现
* @createDate 2025-07-07 16:40:40
*/
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo>
    implements UserinfoService{

    @Resource
    private UserinfoMapper userinfoMapper;

    @Resource
    private UserInfoRepository userInfoRepository;

    @Resource
    private UserMapper userMapper;

    @Resource
    private EmailService emailService;


    @Override
    public UserInfoVO getUserInfoByUserId(Integer userId) {
        return userinfoMapper.getUserInfoByUserId(userId);
    }


    @Override
    public PageResult<UserInfoVO> searchUserInfo(PageParam pageParam) {
        Page<UserInfoVO> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        IPage<UserInfoVO> resultPage = userinfoMapper.searchPage(page, pageParam.getKeyword());
        return new PageResult<>(resultPage);
    }

    public void findUserinfoIdsWithBirthdayIn7Days(){
        List<Userinfo> userinfoList = userInfoRepository.findUsersWithBirthdayIn7Days();

        for (Userinfo userinfo : userinfoList) {
            sendEmailNotification(userinfo);
        }

    }

    @Async
    public void sendEmailNotification(Userinfo userinfo){
        User user = userMapper.selectById(userinfo.getUserId());
        String subject = "会员生日提醒";

        String text = "管理员您好：" +
                "会员："+userinfo.getName()
                +"将于七日后生日！";
        //todo 该邮箱会总管理员邮箱
        emailService.sendSimpleMessage(user.getEmail(), subject, text);
    }

}




